#!/bin/sh
#
# An example hook script to check the commit log message.
# Called by git-commit with one argument, the name of the file
# that has the commit message.  The hook should exit with non-zero
# status after issuing an appropriate message if it wants to stop the
# commit.  The hook is allowed to edit the commit message file.
#
# To enable this hook, make this file executable.

# Uncomment the below to add a Signed-off-by line to the message.
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"

# This example catches duplicate Signed-off-by lines.

test "" = "$(grep '^Signed-off-by: ' "$1" |
    sort | uniq -c | sed -e '/^[   ]*1[    ]/d')" || {
    echo >&2 Duplicate Signed-off-by lines.
    exit 1
}

# Make sure commits on RHEL branches reference RHEL bugs.
RETVAL=0

branch_name=$(git branch | grep ^* | cut -c3-)
branch_pattern="^rhel([[:digit:]])-(.*)"
if [[ ! "$branch_name" =~ $branch_pattern ]]; then
    exit ${RETVAL}
fi
RELEASE=${BASH_REMATCH[1]}

# Is this a local branch, or the primary branch?
if [ "${BASH_REMATCH[2]}" == "branch" ]; then
    # Make a missing ack block the commit
    ACK_FATAL=1
else
    # Missing acks are just warnings
    ACK_FATAL=0
fi

if [ -f "${HOME}/.rhbzauth" ]; then
    . "${HOME}/.rhbzauth"
fi

if [ -z "${RHBZ_USER}" -o -z "${RHBZ_PASSWORD}" ]; then
    bzcmd="bugzilla"
else
    bzcmd="bugzilla --user=${RHBZ_USER} --password=${RHBZ_PASSWORD}"
fi

${bzcmd} >/dev/null 2>&1
if [ $? -eq 127 ]; then
    echo "*** 'dnf install python-bugzilla' to validate bug references." >&2

    msg="$(mktemp $(pwd)/commit.msg.XXXXXXXXXX)"
    cp "${1}" "${msg}"
    echo
    echo "Aborted commit message written to: $(basename ${msg})"
    exit 1
else
    ${bzcmd} login
fi

# Check the bz to see if it has an ack for this release. If the branch is
# a local branch, just warn. If it is the primary branch, block the commit.
bz_has_ack() {
    bug=$1
    flags=$(${bzcmd} query --bug_id=${bug} --outputformat="%{flags}")

    ack_pattern="rhel-${RELEASE}\.[[:digit:]]+\.[[:digit:]]+\+"
    if [[ ! "$flags" =~ $ack_pattern ]]; then
        echo "*** BZ ${bug} is missing acks: ${flags}"

        if [ $ACK_FATAL -eq 1 ]; then
            RETVAL=1
        fi
    fi
}

summary="$(head -n 1 ${1})"
for word in ${summary} ; do
    echo "${word}" | grep -q -E "^.*(#[0-9]+).*"
    if [ $? -eq 0 ]; then
        bug="$(echo "${word}" | sed -e 's/^(#//g' -e 's/).*$//g')"
        ${bzcmd} query --bug_id=${bug} --outputformat="%{product}" | grep -q "^Red Hat Enterprise Linux.*"
        if [ $? -ne 0 ]; then
            echo "*** BZ ${bug} is not a RHEL bug." >&2
            RETVAL=1
        fi
        bz_has_ack ${bug}
    fi
done

last=$(($(wc -l < ${1}) - 2))
if [ ${last} -gt 0 ]; then
    tail -n ${last} ${1} | grep -v "^#" |
    grep -E "^(Resolves|Related|Conflicts): rhbz#[0-9]+$" |
    while read line ; do
        bug="$(echo ${line} | cut -d '#' -f 2)"
        ${bzcmd} query --bug_id=${bug} --outputformat="%{product}" | grep -q "^Red Hat Enterprise Linux.*"
        if [ $? -ne 0 ]; then
            echo "*** BZ ${bug} is not a RHEL bug." >&2
            RETVAL=1
        fi
        bz_has_ack ${bug}
    done
fi

if [ ${RETVAL} -eq 1 ]; then
    msg="$(mktemp $(pwd)/commit.msg.XXXXXXXXXX)"
    cp "${1}" "${msg}"
    echo
    echo "Aborted commit message written to: $(basename ${msg})"
fi

exit ${RETVAL}
